package api;

import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DesTestMethod {
	
	private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};

	 

	   public static byte[]encryptDES(String encryptString, String encryptKey)

	           throws Exception {

	 

	       System.out.println("willencryptedData with UTF-8 encoding =" +parseByte2HexStr(encryptString.getBytes("UTF-8")));

	      

	       IvParameterSpec zeroIv =new IvParameterSpec(iv);

	       SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");

	       Cipher cipher =Cipher.getInstance("DES/CBC/PKCS5Padding");

	       cipher.init(Cipher.ENCRYPT_MODE,key, zeroIv);

	       byte[] encryptedData =cipher.doFinal(encryptString.getBytes("UTF-8"));

	       System.out.println("didencryptedData  =" +parseByte2HexStr(encryptedData));

	       return encryptedData;

	   }

	 

	   public static String encryptDESwithBase64(String encryptString,String encryptKey) throws Exception

	   {

	       return Base64Test.encode(encryptDES(encryptString,encryptKey));

	   }

	//JAVA中DES解密实现方法如下：

	   public static String decryptDES(byte[] encryptedData, String decryptKey)

	           throws Exception {

	       System.out.println("willdecryptedData =" + parseByte2HexStr(encryptedData));

	      

	       IvParameterSpec zeroIv =new IvParameterSpec(iv);

	       SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes("UTF-8"), "DES");

	       Cipher cipher =Cipher.getInstance("DES/CBC/PKCS5Padding");

	       cipher.init(Cipher.DECRYPT_MODE,key, zeroIv);

	       byte decryptedData[] =cipher.doFinal(encryptedData);

	 

	       System.out.println("diddecryptedData with UTF-8 encoding =" + parseByte2HexStr(decryptedData));

	 

	       String decryptedString =new String(decryptedData, "UTF-8");

	       System.out.println("diddecryptedString with UTF-8 encoding =" + decryptedString);

	       return decryptedString;

	   }

	   public static String decryptDESwithBase64(String encryptedString, String decryptKey)  throws Exception

	   {

	       byte[]encryptedData=Base64Util.getFromBase64(encryptedString).getBytes();

	       return decryptDES(encryptedData, decryptKey);      

	   }  
	   
	   public static void main(String[] args) throws Exception {

	       String plainText ="abcdefghihjjjkelaemn";

	       String keyText =Constant.KEY_DES;

	 

	       plainText = "{\"errcode\":0,\"data\":{\"news\":[],\"totalPage\":1,\"lunbo\":[],\"category\":1,\"pageNow\":1},\"errmsg\":\"请求成功\"}";


	 

	       byte[] encryptedData =encryptDES(plainText, keyText);

	       String decryptedString=decryptDES("idLF4mtajlCOEkgnRJI7K9hg3gItO/iUxN4jqyAvhvUfvF7IvLquz2NmXGR+XIq61wh6wSFKNmTIM3W0xWjZqnvv76z9B+TnSYj++9DkQEYXmJbF90gGQV0vkXi+K4W0qFXxFTgOVNXrVwZZhs0O2BnaIQbgYHgGXvATquhUSDNsLVIJ6zi8qXxJI0VVcrXxP2Mmew7v2h6T2DNzAeVFL/XVZkLkEGLSHaCOrOXiLBeCMit/EIET+hH6VPAF8+hmpCdBkDoM0MrK76oK74LGdaAV6ZIE5GDcv3MZxrz2ZwtvEheCQ8jjLMXvShn04mm6lvcyv8FG7nhjmZBzdjJTQQ==".getBytes(), keyText);
/*	       String decryptedString=decryptDES(encryptedData, keyText);
*/
	      

	       String cipherText =parseByte2HexStr(encryptedData);

	      

	       System.out.println("明文：" + plainText);

	       System.out.println("密钥：" + keyText);

	       System.out.println("密文 Base64 编码：" + cipherText);
	       String strBase64=new BASE64Encoder().encode(encryptedData);
	       System.out.println("密文数组直接base64："+ strBase64);
	       System.out.println("密文数组直接base64再Base64:"+new BASE64Encoder().encode(strBase64.getBytes("UTF-8")) );
	       System.out.println("解密后：" + decryptedString);    
	       
	       
	       
	      /*

	       String encryptedString =encryptDESwithBase64(plainText, keyText);

	       decryptedString=decryptDESwithBase64(encryptedString,keyText);

	      

	       System.out.println("明文：" + plainText);

	       System.out.println("密钥：" + keyText);

	       System.out.println("密文：" + encryptedString);

	       System.out.println("解密后：" + decryptedString);*/

	   }
	   
	   public static String parseByte2HexStr(byte buf[]) {  
		           StringBuffer sb = new StringBuffer();  
		           for (int i = 0; i < buf.length; i++) {  
		                   String hex = Integer.toHexString(buf[i] & 0xFF);  
		                   if (hex.length() == 1) {  
		                           hex = '0' + hex;  
		                   }  
		                   sb.append(hex);  
		           }  
		           return sb.toString();  
		   }

}
